package person.learn.controller.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;
import person.learn.entity.Demo;
import person.learn.entity.GoodsEnum;
import person.learn.entity.Product;
import person.learn.jacksonmessageconverter.JSONFormat;
import person.learn.conf.http.HttpRequestHandler;
import person.learn.entity.Goods;
import person.learn.service.GoodsService;
import person.learn.util.Constant;
import person.learn.util.JsonResponse;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by barton on 16-1-29.
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private HttpRequestHandler handler;

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<JsonResponse<Page<Goods>>> page(HttpServletRequest request,
                                                          @PageableDefault(size = 10, page = 0,
                                                                  sort = {"id", "name"},
                                                                  direction = Sort.Direction.ASC)
                                                                  Pageable pageRequest) {

        Map<String, Object> searchParam = WebUtils.getParametersStartingWith(request, Constant
                .SEARCH_OPERTOR);

        return new ResponseEntity<>(new JsonResponse<>(this.goodsService.findAllPage(searchParam,
                pageRequest)), HttpStatus.OK);
    }

    @RequestMapping("/all")
    @JSONFormat(filterField = "Product.name", enumMethodName = "GoodsEnum.getName")
    public ResponseEntity<JsonResponse<List<Goods>>> findAll() {
        List<Goods> list = new ArrayList<>();
        Goods goods1 = new Goods();
        goods1.setId(22l);
        goods1.setName("商品");
        goods1.setCount(100);
        goods1.setDescription("我是商品描述");
        goods1.setCreateDate(new Date());
        goods1.setGoodsEnum(GoodsEnum.MOBILE);

        Product product1 = new Product();
        product1.setId(23l);
        product1.setName("product1");

        Product product2 = new Product();
        product2.setId(24l);
        product2.setName("product2");

        List<Product> products = new ArrayList<>();
        products.add(product1);
        products.add(product2);

        goods1.setProducts(products);


        Demo demo = new Demo();

        demo.setId(99);
        demo.setName("DEMO");
        demo.setModifiedDate(new Date());

        goods1.setDemo(demo);

        list.add(goods1);

        return new ResponseEntity<>(new JsonResponse(list),HttpStatus.OK);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @JSONFormat(dateFormat = "yyyy/MM/dd HH:mm:ss", filterField = {"product.id"}/*,
    filterCollection =true*/)
    public Goods findOneById(@PathVariable Long id) {
        /*return this.goodsService.findOne(id);*/

        Goods goods = new Goods();
        goods.setId(22l);
        goods.setName("商品");
        goods.setCount(100);
        goods.setDescription("我是商品描述");
        goods.setCreateDate(new Date());

        Product product1 = new Product();
        product1.setId(23l);
        product1.setName("product1");

        Product product2 = new Product();
        product2.setId(24l);
        product2.setName("product2");

        List<Product> list = new ArrayList<>();
        list.add(product1);
        list.add(product2);

        goods.setProducts(list);


        Demo demo = new Demo();

        demo.setId(99);
        demo.setName("DEMO");
        demo.setModifiedDate(new Date());

        goods.setDemo(demo);

        return goods;
    }

    /**
     * 测试
     *
     * @return
     */
    @RequestMapping(value = "/toFruit", method = RequestMethod.GET)
    public String toFruit() {
        ResponseEntity<Object> re = handler.get("http://localhost:8443/v1/fruits");
        System.out.println(re.getStatusCode());
        System.out.println(re.getBody());
        System.out.println(re.getHeaders());
        return "fruit";
    }
}
